package org.xukai.rest.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;
import org.xukai.dal.dataobject.GroupDO;
import org.xukai.rest.model.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

//@RestController("restful/user")
@Controller
@RequestMapping("restful/user")
public class RestUserController {

    @Resource
    protected HttpServletRequest request;



    private static final Logger logger = Logger.getLogger(RestUserController.class);
    /**
     * 根据用户id查询用户数据
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    @ResponseBody
    public ResponseEntity<String> queryUserById(@PathVariable("id") String id) {
        System.out.println(id + "====" + request.getHeader("uid") + "hashcode---" + request.hashCode());
        logger.info(id + "====-----------" + request.getHeader("uid"));
        return  ResponseEntity.status(HttpStatus.CREATED).body("adadd");
    }

    /**
     * 新增用户
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "saveUser",method = RequestMethod.POST)
    public ResponseEntity<Void> saveUser(@Valid GroupDO user, Errors errors) {
        try {
//            this.newUserService.saveUser(user);
            boolean b = errors.hasErrors();
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 更新用户资源
     * 
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> updateUser(String user) {
        try {
//            this.newUserService.updateUser(user);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 删除用户资源
     * 
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {
        try {
            if (id.intValue() == 0) {
                // 请求参数有误
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
//            this.newUserService.deleteUserById(id);
            // 204
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}
